<template>
  <el-dialog
    title="设置权限"
    :visible="showDialogPerm"
    width="40%"
    :before-close="handleClose"
  >
    <el-tree
      ref="tree"
      node-key="id"
      :data="permList"
      show-checkbox
      default-expand-all
      check-strictly
      :props="{ label: 'name' }"
    />
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="hEdit">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getPermission } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
import { getPermissById, assignPerm } from '@/api/role'
export default {
  props: {
    showDialogPerm: {
      type: Boolean,
      required: true
    },
    codId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      defaultProps: {
        lable: 'name'
      },
      permIds: [],
      permList: []
    }
  },
  created() {
    this.loadPerms()
  },
  methods: {
    // 分配员工角色
    async hEdit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.codId, permIds: permIds })
      this.$message.success('权限设置成功')
      this.handleClose()
    },
    // 回显已有的权限
    async getPermUser() {
      const res = await getPermissById(this.codId)
      console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 获取权限架构
    async loadPerms() {
      const res = await getPermission()
      console.log(res)
      this.permList = transListToTreeData(res.data, 0)
    },
    // 关闭
    handleClose() {
      this.$emit('update:showDialogPerm', false)
    }
  }
}
</script>

<style>

</style>
